package com.platform.api.bzproject.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.platform.api.bzproject.model.BzClassification;
import com.platform.api.bzproject.model.BzConfig;
import com.platform.api.bzproject.model.UserClassify;
import com.platform.api.bzproject.service.BzConfigService;
import com.platform.api.bzproject.service.IUserClassifyService;
import com.platform.api.bzproject.vo.UserClassifyVo;
import com.platform.common.core.Constant;
import com.platform.common.core.Result;
import com.platform.security.util.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户自定义分类表 前端控制器
 * </p>
 *
 * @author guoshao
 * @since 2022-01-14
 * 源码联系微信：2549789059
 */
@RestController
@RequestMapping("{appid}/u/classify")
public class UserClassifyController {


    @Autowired
    private IUserClassifyService iUserClassifyService;

    @Autowired
    private BzConfigService bzConfigService;

    /**
     * 新增用户自定义分类
     * @return
     */
    @PostMapping("addUserClassify")
    public Result addUserClassify (@RequestBody UserClassify userClassify){
        String userId = null;
        try {
            userId = SecurityUtils.getUser().getUserId();
        }catch (Exception e){
            return Result.failure("操作失败");
        }

        if(null == userClassify.getId()){

            BzConfig byType = bzConfigService.getByType(Constant.TWO_CLASS_COUNT);

            int twoCount = null == byType ? Constant.TWO_CLASS_COUNT_DEFAULT : Integer.valueOf(byType.getValue());

            userClassify.setUserId(userId);
            userClassify.setStatus(0);
            int count = iUserClassifyService.count(
                    new QueryWrapper<UserClassify>().eq("user_id", userId)
            .eq("parent_id",userClassify.getParentId()));

            if(count >= twoCount){
                return Result.failure("添加失败,每个大类下只允许"+twoCount+"个二级分类");
            }

            iUserClassifyService.save(userClassify);
            return Result.success();
        }

        UserClassify classify = iUserClassifyService.getById(userClassify.getId());
        classify.setName(userClassify.getName());
        classify.setStatus(0);
        iUserClassifyService.updateById(classify);
        return Result.success();
    }

    /**
     * 新增用户自定义分类
     * @return
     */
    @GetMapping("delUserClassify/{id}")
    public Result delUserClassify (@PathVariable Long id){
        iUserClassifyService.removeById(id);
        return Result.success();
    }

    /**
     * 用户自定义分类
     * @return
     */
    @GetMapping("userClassifiList")
    public Result userClassifiList (UserClassify userClassify){

        String userId = null;
        try {
            userId = SecurityUtils.getUser().getUserId();
            userClassify.setUserId(userId);
        }catch (Exception e){

        }

        LambdaQueryWrapper<UserClassify>  lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(UserClassify::getId);
        lambdaQueryWrapper.eq(null != userClassify.getParentId(), UserClassify::getParentId,userClassify.getParentId());
        lambdaQueryWrapper.eq(null != userClassify.getStatus(), UserClassify::getStatus,userClassify.getStatus());
        lambdaQueryWrapper.eq(null != userClassify.getUserId(), UserClassify::getUserId,userClassify.getUserId());

        List<UserClassify> userClassifies = iUserClassifyService.list(lambdaQueryWrapper);

        return Result.success(userClassifies);
    }

    /**
     * 用户自定义分类
     * @return
     */
    @GetMapping("classList")
    public Result classList (UserClassify userClassify){

        String userId = null;
        try {
            userId = SecurityUtils.getUser().getUserId();
            userClassify.setUserId(userId);
        }catch (Exception e){

        }

        QueryWrapper<UserClassify>  queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("parent_id",userClassify.getParentId());
        queryWrapper.eq("user_id",userId);

        List<UserClassify> userClassifies = iUserClassifyService.list(queryWrapper);
        List<UserClassifyVo> list = Lists.newArrayList();

        for (int i = 0; i < userClassifies.size(); i++) {
            UserClassifyVo userClassifyVo = new UserClassifyVo();
            userClassifyVo.setId(userClassifies.get(i).getId());
            userClassifyVo.setName(userClassifies.get(i).getName());
            list.add(userClassifyVo);
        }

        return Result.success(list);
    }

}
